package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"t-go-zero/service/user/rpc/internal/svc"
	"t-go-zero/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type ARoleUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewARoleUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ARoleUpdateLogic {
	return &ARoleUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ARoleUpdateLogic) ARoleUpdate(in *userclient.ARoleUpdateReq) (*userclient.CommonResp, error) {

	res, err := l.svcCtx.ARoleModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if err == sqlc.ErrNotFound {
			return nil, fmt.Errorf("ARole没有该ID:%v", in.Id)
		}
		return nil, err
	}

	// 角色名称
	if len(in.Name) > 0 {
		res.Name = in.Name
	}
	// 备注
	if len(in.Remark) > 0 {
		res.Remark.String = in.Remark
		res.Remark.Valid = true
	}

	err = l.svcCtx.ARoleModel.Update(l.ctx, res)

	if err != nil {
		return nil, err
	}
	return &userclient.CommonResp{}, nil
}
